package com.app.shop.controller.system;

import com.app.shop.util.ApiRest;
import com.app.shop.util.Constant;
import com.app.shop.util.SysModel;
import com.app.shop.entity.SysMenu;
import com.app.shop.entity.query.SysMenuQuery;
import com.app.shop.config.security.DgbSecurityUserHelper;
import com.app.shop.service.system.SysMenuService;
import com.app.shop.entity.show.SysMenuShow;
import com.app.shop.util.ClassUtil;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 菜单服务类Controller
 *
 * @author  chen li wei
 * @version 21:14 2019-5-10
 */
@Controller
@RequestMapping(value = "/sysMenu")
public class MenuController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(MenuController.class);
    @Autowired
    SysMenuService sysMenuService;

    /**
     * 查询菜单列表
     * @param sysMenuQuery 查询菜单参数
     * @return 返回菜单列表
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/getMenuList")
    public ApiRest getDictList(@RequestBody SysMenuQuery sysMenuQuery) {
        log.debug(JSONObject.toJSON(sysMenuQuery).toString());
        //查询
        List<SysMenu> sysMenuList=sysMenuService.getSysMenuList(sysMenuQuery);
        //查询结果对象置换
        List<SysMenuShow> resList=new ArrayList<SysMenuShow>();
        SysMenuShow sysMenuShow;
        //数据转换
        for(SysMenu sysMenu:sysMenuList){
            sysMenuShow=new SysMenuShow();
            try {
                ClassUtil.fatherToChild(sysMenu,sysMenuShow);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //码表值补全
            sysMenuShow.dataSet();
            resList.add(sysMenuShow);
        }
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE,resList);
    }

    /**
     * 菜单信息查询
     *
     * @param sysMenuQuery 需要查询的菜单信息
     * @return 菜单信息
     */
    @ResponseBody
    @RequestMapping(value = "getMenuInfo")
    public ApiRest getMenuInfo(@RequestBody SysMenuQuery sysMenuQuery) {
        log.debug(JSONObject.toJSON(sysMenuQuery).toString());
        SysMenuShow sysMenuShow = sysMenuService.getMenuInfo(sysMenuQuery);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, sysMenuShow);
    }


    /**
     * 菜单信息添加
     *
     * @param sysMenu 需要添加的菜单信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "addMenuInfo")
    public ApiRest addMenuInfo(@RequestBody SysMenu sysMenu) {
        log.debug(JSONObject.toJSON(sysMenu).toString());
        sysMenu.setCreateUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        sysMenu.setUpdateUserId(sysMenu.getCreateUserId());
        sysMenu.setVersion(new Date());
        SysModel sysModel = sysMenuService.addMenuInfo(sysMenu);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 菜单信息修改
     *
     * @param sysMenu 需要修改的菜单信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "updateMenuInfo")
    public ApiRest updateMenuInfo(@RequestBody SysMenu sysMenu) {
        log.debug(JSONObject.toJSON(sysMenu).toString());
        sysMenu.setUpdateUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        SysModel sysModel = sysMenuService.updateMenuInfo(sysMenu);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }
    
    /**
     * 菜单禁用
     *
     * @param sysMenuQuery 需要禁用的菜单信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "updateMenuStop")
    public ApiRest updateMenuStop(@RequestBody SysMenuQuery sysMenuQuery) {
        log.debug(JSONObject.toJSON(sysMenuQuery).toString());
        sysMenuQuery.setOptionUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        sysMenuService.updateMenuStop(sysMenuQuery);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
    }



}
